package com.youxin.busfoundation.sms;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.foundation.utils.StringUtils;


/**
 * Sms短信处理
 * 
 * @author yingde.cao
 *
 */
public class SmsUtils {
	private final static String INF_URL = "http://q.hl95.com:8061";
	private final static String USER_NAME = "sls01";
	private final static String PASSWORD = "YouXin15973";
	private final static String EPID = "121629";
	private final static String SINGATURE="【商理事】";
	public final static String SUCCESS_MESSAGE="00";
	
	public static ResponseObject send(String phone,String message) throws Exception
	{
		String result=sendSms(phone, message);
		if(SUCCESS_MESSAGE.equals(result))
		{
			return new ResponseObject();
		}
		return ResponseObject.userFailure("短信发送到["+phone+"]失败,"+"失败信息:"+StringUtils.isNullSetEmpty(result));
	}
	
	public static String sendSms(String phone,String message) throws Exception {		
		/**
		 * 	商户唯一标示20位长度
		 */
		String linkid = "";
		String subcode = "";
		message=SINGATURE+message;
		Map<String, String> pmap = new HashMap<String, String>(7);
		pmap.put("username", USER_NAME);
		pmap.put("password", PASSWORD);
		pmap.put("phone", phone);
		pmap.put("message", URLEncoder.encode(message, "gb2312"));
		pmap.put("epid", EPID);
		pmap.put("linkid", linkid);
		pmap.put("subcode", subcode);
		String pstr = "";
		if (pmap != null && pmap.size() > 0) {
			for (Map.Entry<String, String> entry : pmap.entrySet()) {
				pstr += "&" + entry.getKey() + "=" + entry.getValue();
			}
			pstr = pstr.substring(1);
		}
		String result = sendGet(INF_URL + "?" + pstr);
		return result;
	}

	private static String sendGet(String url) throws Exception{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		BufferedReader in = null;
		try {
			HttpGet get = new HttpGet(url);
			CloseableHttpResponse httpResponse = null;
			httpResponse = httpClient.execute(get);
			try {
				HttpEntity entity = httpResponse.getEntity();
				if (null != entity) {
					String tempStr = "";
					in = new BufferedReader(new InputStreamReader(
							entity.getContent()));
					StringBuffer content = new StringBuffer();
					while ((tempStr = in.readLine()) != null) {
						content.append(tempStr);
					}
					return content.toString();
				}
			}
			finally {
				httpResponse.close();
			}

		} catch (Exception e) {
			throw e;
		}
		finally {
			try {
				if (in != null) {
					in.close();
				}
				if (httpClient != null) {
					httpClient.close();
				}
			} catch (Exception e) {
				throw e;
			}
		}
		return "";
	}
	
	

}
